package cn.dnui_zzh807.dnuifood_16110100807;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_zzh807.dnuifood_16110100807.bean.LoginBean;
import cn.dnui_zzh807.dnuifood_16110100807.controller.FragmentActivity;
import cn.dnui_zzh807.dnuifood_16110100807.controller.Signin;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.model.UserModel;
import cn.dnui_zzh807.dnuifood_16110100807.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Dobby on 2018/11/26.
 */

public class MainActivity  extends AppCompatActivity implements OnRetrofitListener<LoginBean> {
    private EditText username,userpass;
    private Button btn_login,btn_signin;
    private String user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvents();

    }

    public void initView(){
        username = (EditText)findViewById(R.id.name);
        userpass = (EditText)findViewById(R.id.pw);
        btn_login = (Button)findViewById(R.id.button);
        btn_signin = (Button)findViewById(R.id.button2);

    }
    private void initEvents(){
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (username.getText().toString().equals("")||userpass.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this,"用户名or密码不能为空",Toast.LENGTH_SHORT).show();
                }else {
//                    login1(username.getText().toString(),userpass.getText().toString());
                    login();
                }
            }
        });
        btn_signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Signin.class);
//                startActivityForResult(intent,1);
                startActivity(intent);
            }
        });
    }

    public void login1(String username,  String userpass){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService = retrofit.create(UserService.class);
        Call<LoginBean> call = userService.userLogin(username,userpass);
        Callback<LoginBean> callback = new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                user_id = response.body().getUserid();
                if ("0".equals(user_id))
                    Toast.makeText(MainActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();
                else {
                    Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this,FragmentActivity.class);
                    startActivity(intent);
                    MainActivity.this.finish();
                    saveUser(user_id);
                }
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {

            }
        };
    }
    private void login(){
        UserModel userModel = new UserModel();
        userModel.userLogin(username.getText().toString().trim(),userpass.getText().toString().trim(),this);

    }
    @Override
    public void onSuccess(LoginBean userId,int flag){
        // String user_id = String.valueOf(userId.getUserid());
        if (userId.getUserid().equals("0")){
            Toast.makeText(MainActivity.this,"用户名或则密码错误",Toast.LENGTH_SHORT).show();
        }else {
            saveUser(userId.getUserid());
            Intent intent = new Intent(MainActivity.this,FragmentActivity.class);
            startActivity(intent);
            Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void onFail() {

        Toast.makeText(MainActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
    }

    public void saveUser(String user_id){
        SharedPreferences sharedPreferences = getSharedPreferences("zzh", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编译器
        editor.putString("username",username.getText().toString());
        editor.putString("user_id",user_id);
//        int userid = Integer.parseInt(user_id);
//        editor.putInt("user_id",userid);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();
    }


    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent date){
        super.onActivityResult(requestCode,resultCode,date);
        String et_user=date.getStringExtra("user");
        username.setText(et_user);
    }

}
